<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduEnterOrder;

/**
 * EduEnterOrderSearch represents the model behind the search form of `backend\models\EduEnterOrder`.
 */
class EduEnterOrderSearch extends EduEnterOrder
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'gift_id', 'status', 'style'], 'integer'],
            [['openid', 'out_trade_no', 'name', 'phone', 'address', 'logistics', 'order_num', 'info', 'remark'], 'safe'],
            [['create_time'], 'string'],
            [['logistics_fee'], 'number'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduEnterOrder::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => ['create_time' => SORT_DESC]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'gift_id' => $this->gift_id,
            'total_fee' => $this->total_fee,
            'count' => $this->count,
            'status' => $this->status,
            'logistics_fee' => $this->logistics_fee,
            'style' => $this->style,
        ]);

        if ($this->create_time != '') {
            $query->andFilterWhere(['between', 'create_time', strtotime($this->create_time), strtotime($this->create_time) + 24 * 60 * 60]);
        }

        $query->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'out_trade_no', $this->out_trade_no])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'address', $this->address])
            ->andFilterWhere(['like', 'logistics', $this->logistics])
            ->andFilterWhere(['like', 'order_num', $this->order_num])
            ->andFilterWhere(['like', 'info', $this->info])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }


}
